package com.metaera.app.ui.core;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import com.metaera.app.R;
import com.metaera.app.controller.view.core.IStatusView;
import com.metaera.app.widget.LayoutRequestStatusView;

import and.fast.statelayout.FailureStateView;
import and.fast.statelayout.OnAnewRequestNetworkListener;
import and.fast.statelayout.StateLayout;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public abstract class BaseRequestFragment extends BaseFragment implements OnAnewRequestNetworkListener, IStatusView {

//    protected P mPresenter;
    protected StateLayout mNetworkStateLayout;
    private   IStatusView mRequestStateView;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (container != null && mNetworkStateLayout == null) {
            mNetworkStateLayout = new StateLayout(container.getContext());
            mNetworkStateLayout.setOnAnewRequestNetworkListener(this);
            mNetworkStateLayout.addStateView(new FailureStateView(R.layout.view_state_layout_failure));
            mNetworkStateLayout.addView(super.onCreateView(inflater, mNetworkStateLayout, savedInstanceState));
        }

        return mNetworkStateLayout;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mRequestStateView = createRequestView();
    }

    protected IStatusView createRequestView() {
        return new LayoutRequestStatusView(mNetworkStateLayout);
    }

    @Override
    public void loading() {
        mRequestStateView.loading();
    }

    @Override
    public void offline() {
        mRequestStateView.offline();
    }

    @Override
    public void failure(Throwable throwable) {
        mRequestStateView.failure(throwable);
    }

    @Override
    public void complete() {
        mRequestStateView.complete();
    }

    @Override
    public void empty(CharSequence charSequence) {
        mRequestStateView.empty(charSequence);
    }

    @Override
    public void onAnewRequestNetwork() {
        //mPresenter.anewRequest();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        //mPresenter.onHiddenChanged(hidden);
    }

}
